package com.suep.business.mapper;

import com.suep.business.domain.Coupon;
import com.suep.business.dto.CouponDTO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface CouponMapper {

    //用户查询可以领取的优惠券表
    @Select("SELECT id,code, name, coupon.description, type, discount, min_order_amount, expiry_date, total_count, remain_count, coupon.status, store_name, coupon.create_time, coupon.update_time \n" +
            "FROM coupon, store \n" +
            "WHERE coupon.status = 1 AND coupon.store_id = store.store_id; ")
    List<CouponDTO> selectAllCoupon(LocalDateTime currentDate);

    //用户查询自己拥有的优惠券而且未使用过的
    @Select("SELECT coupon.id,code, name, coupon.description, type, discount, min_order_amount, expiry_date, total_count, remain_count, coupon.status, store_name, coupon.create_time, coupon.update_time \n" +
            "FROM user_coupon,coupon,store where user_coupon.user_id=#{userId} and user_coupon.status='未使用' and coupon.id=user_coupon.coupon_id and coupon.store_id=store.store_id")
    public List<CouponDTO> selectMyCoupon(@Param("userId") Integer userId);

    //用户使用自己的优惠券
    @Update("UPDATE user_coupon SET  status='已使用',use_time=#{useTime} WHERE user_id=#{userId}")
    public Integer useMyCoupon(@Param("userId") Integer userId,@Param("useTime") LocalDateTime useTime);


    //用户领取优惠券后，用户与优惠券关系表插入信息
    @Insert("INSERT INTO user_coupon ( user_id,coupon_id,status) VALUES(#{userId},#{couponId},'未使用')")
    Integer insertUserAndCoupon(@Param("userId") Integer userId,@Param("couponId") Integer couponId);

    // 主键精确查询
    @Select("SELECT id, code, name, description, type, discount, min_order_amount, expiry_date, total_count, remain_count, status, store_id, create_time, update_time FROM coupon WHERE id = #{id}")
    Coupon selectCouponById(@Param("id") Integer id);

    // 外键查询
    @Select("SELECT id, code, name, description, type, discount, min_order_amount, expiry_date, total_count, remain_count, status, store_id, create_time, update_time FROM coupon WHERE store_id = #{storeId}")
    List<Coupon> selectCouponByStoreId(@Param("storeId") Integer storeId);

    // 主键精确删除
    @Delete("DELETE FROM coupon WHERE id = #{id}")
    Integer deleteCouponById(@Param("id") Integer id);

    // 插入操作
    @Insert("INSERT INTO coupon (code, name, description, type, discount, min_order_amount, expiry_date, total_count, remain_count, status, store_id, create_time, update_time) " +
            "VALUES (#{code}, #{name}, #{description}, #{type}, #{discount}, #{minOrderAmount}, #{expiryDate}, #{totalCount}, #{remainCount}, #{status}, #{storeId}, #{createTime}, #{updateTime})")
    Integer insertCoupon(Coupon coupon);

    // 更新操作
    @Update("UPDATE coupon SET code = #{code}, name = #{name}, description = #{description}, type = #{type}, discount = #{discount}, min_order_amount = #{minOrderAmount}, " +
            "expiry_date = #{expiryDate}, total_count = #{totalCount}, remain_count = #{remainCount}, status = #{status}, store_id = #{storeId}, create_time = #{createTime}, update_time = #{updateTime} WHERE id = #{id}")
    Integer updateCouponById(Coupon coupon);
}